
package com.yuan.graduate;

import com.yuan.application.BaseActivity;
import com.yuan.control.CheckControl;
import com.yuan.dialog.CustomProgressDialog;
import com.yuan.dialog.DialogUtil;
import com.yuan.model.M请假申请;
import com.yuan.unit.AsyncDataJobHandler;
import com.yuan.unit.ToastHelper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

/**
 * 请假界面
 * 
 * @author YUAN
 */
@SuppressLint("HandlerLeak")
@SuppressWarnings("unused")
public class LeaveActivity extends BaseActivity {
    private Context mContext = null;
    private TextView leavestateinfo = null, leave_type = null;
    private EditText leave_reason = null, time_start = null, time_end = null;
    private Button leave_submit = null;
    private CustomProgressDialog mProgressDialog = null;
    private CustomProgressDialog mCustomProgressDialog = null;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what)
            {
                case LEAVETYPEFROMSQL:
                    List<?> list = (List<?>) msg.obj;
                    DialogUtil.createLeaveTypeDialog(mContext, mHandler, list).show();
                    break;
                case LEAVETYPE:
                    String type = (String) msg.obj;
                    leave_type.setText(type);
                    break;
                case ASKFORLEAVESUCCEED:
                    ToastHelper.getInstance().shortToast(mContext, "请假成功，在查看审批几面查看是否被批准");
                    finish();
                    break;
                case ASKFORLEAVEFILDED:
                    ToastHelper.getInstance().shortToast(mContext, "请假失败");
                    break;
                default:
                    break;
            }
        }
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_askleave);
        initView();
    }

    private void initView() {
        // TODO Auto-generated method stub
        leavestateinfo = (TextView) findViewById(R.id.leavestateinfo);
        leave_type = (TextView) findViewById(R.id.leave_type);
        leave_reason = (EditText) findViewById(R.id.leave_reason);
        time_start = (EditText) findViewById(R.id.time_start);
        time_end = (EditText) findViewById(R.id.time_end);
        leave_submit = (Button) findViewById(R.id.leave_submit);
        leave_type.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AsyncDataJobHandler.getInstance().getLeaveType(mHandler);
            }
        });
        leave_submit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String reason = leave_reason.getText().toString().trim();
                String start = time_start.getText().toString().trim();
                String end = time_end.getText().toString().trim();
                if (TextUtils.isEmpty(reason) || TextUtils.isEmpty(start)
                        || TextUtils.isEmpty(end))
                {
                    ToastHelper.getInstance().shortToast(mContext, "输入为空");
                    return;
                }
                if (CheckControl.isLeaveRightTime(start) && CheckControl.isLeaveRightTime(end))
                {
                    M请假申请.getInstance().set开始(start + ":00.0");
                    M请假申请.getInstance().set结束(end + ":00.0");
                    M请假申请.getInstance().set请假理由(reason);
                    AsyncDataJobHandler.getInstance().AskLeave(mHandler);
                } else
                {
                    ToastHelper.getInstance().shortToast(mContext, "输入时间格式错误");
                    return;
                }
            }
        });
    }
}
